package com.zx.viewlearn.views;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Shader;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import com.zx.viewlearn.R;

/**
 * Created by zhoux on 2018/1/8.
 * 注释：自定义view onMeasure onDraw学习
 */

public class MyViewMusic extends View {
    Paint mPaint;
    final static int minWidth = 600;

    public MyViewMusic(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);

        mPaint = new Paint();
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setColor(getResources().getColor(R.color.red));
    }

    int mRectCount = 10;
    int width = 25;
    int curHeight = 180;
    int offset = 10;
    int mRectHeight;
    int mRectWidth;

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mRectWidth = getWidth();
        mRectHeight = getHeight();
        mRectCount = (mRectWidth / (width + offset) - 1);//个数
        LinearGradient mLinearGradient = new LinearGradient(0, 0, 0, mRectHeight, new int[]{Color.RED, Color.YELLOW, Color.BLUE}, null, Shader.TileMode.CLAMP);
        mPaint.setShader(mLinearGradient);

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        setMeasuredDimension(getMySize(widthMeasureSpec), getMySize(heightMeasureSpec));
    }

    @Override
    protected void onDraw(Canvas canvas) {

        drawView(canvas);
    }


    private void drawView(Canvas canvas) {
        for (int i = 0; i < mRectCount; i++) {
            curHeight = getOneHeight(i);
            canvas.drawRect((width + offset) * i + offset, curHeight, (width + offset) * (i + 1), mRectHeight, mPaint);
        }
        this.postInvalidateDelayed(300);
    }

    private int getOneHeight(int i) {
        return curHeight = (int) (Math.random() * mRectHeight);

    }

//    abstract int getRandom();

    public void playMusic(double perHeight) {
        curHeight = (int) (perHeight * mRectHeight);
        this.postInvalidateDelayed(300);
    }

    public static int getMySize(int measureSpec) {
        int result = 0;
        int specMode = MeasureSpec.getMode(measureSpec);
        int specSize = MeasureSpec.getSize(measureSpec);
        Log.e("MyView3", "specMode:" + specMode);
        Log.e("MyView3", "specSize:" + specSize);//当没有给固定值时候(match_parent，wrap_content)，specSize给的是充满父控件的值。
        switch (specMode) {
            case MeasureSpec.EXACTLY://view设置成match_parent时候，会是这个模式.
                Log.e("MyView3", "MeasureSpec.EXACTLY:" + MeasureSpec.EXACTLY);//1073741824
                result = specSize;
                break;
            case MeasureSpec.AT_MOST://view设置成wrap_content时候，会是这个模式
                Log.e("MyView3", "MeasureSpec.AT_MOST:" + MeasureSpec.AT_MOST);//-2147483648
                result = minWidth;
                result = Math.min(result, specSize);//设置成wrap_content时候，给一个默认值，这样就不会充满屏幕了
                break;
            case MeasureSpec.UNSPECIFIED:
                Log.e("MyView3", "MeasureSpec.UNSPECIFIED:" + MeasureSpec.UNSPECIFIED);//0
                result = specSize;
                break;
            default:
                break;
        }
        return result;

    }


}
